package com.zhouyiquan.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhouyiquan.reggie.common.CustomException;
import com.zhouyiquan.reggie.entity.Category;
import com.zhouyiquan.reggie.entity.Dish;
import com.zhouyiquan.reggie.entity.Setmeal;
import com.zhouyiquan.reggie.mapper.CategoryMapper;
import com.zhouyiquan.reggie.service.CategoryService;
import com.zhouyiquan.reggie.service.DishService;
import com.zhouyiquan.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 周益全 (https://gitee.com/zfovikqdev928)
 * @BugFeedBackEmail: zfovikqdev928@126.com
 * @Date: Create in 2022-10
 * @Description:
 * @Version:
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService; //菜品
    @Autowired
    private SetmealService setmealService;//套餐

    /**
     * 根据 id 删除分类
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询是否有关联菜品,如有,抛出业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id); //查询条件
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("当前分类下有关联菜品，无法删除");
        }

        //查询是否有关联套餐,如有,抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count();
        if (count2 > 0) {
            throw new CustomException("当前分类下有关联套餐，无法删除");
        }
        super.removeById(id);
    }
}
